package com.lsw.AddressSelection;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.lsw.R;
import com.lsw.util.LogUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 选择城市
 * Created by lsw on 2016/12/1.
 */
public class AddressSelectionActivity extends Activity {

    private ListView sortListView;
    private SideBar sideBar;
    private TextView dialog;
    private SortAdapter adapter;

    /**
     * 汉字转换成拼音的类
     */
    private CharacterParser characterParser;
    private List<SortModel> SourceDateList;

    /**
     * 根据拼音来排列ListView里面的数据类
     */
    private PinyinComparator pinyinComparator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_selection);
        initViews();
        initData();
    }

    protected void initViews() {
        sideBar = (SideBar) findViewById(R.id.sidrbar);
        dialog = (TextView) findViewById(R.id.dialog);
        sortListView = (ListView) findViewById(R.id.country_lvcountry);
        sideBar.setTextView(dialog);
    }

    protected void initData() {
        //实例化汉字转拼音类
        characterParser = CharacterParser.getInstance();

        pinyinComparator = new PinyinComparator();

        //设置右侧触摸监听
        sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {

            @Override
            public void onTouchingLetterChanged(String s) {
                LogUtils.i("lsw", "=====s=====" + s);
                //该字母首次出现的位置
                int position = adapter.getPositionForSection(s.charAt(0));
                if (position != -1) {
                    sortListView.setSelection(position);
                }

            }
        });

        sortListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
//                Intent intent = new Intent();
//                intent.putExtra("city", SourceDateList.get(position).getName());
//                setResult(RESULT_OK, intent);
//                finish();
            }
        });
        SourceDateList = new ArrayList<>();
        adapter = new SortAdapter(this, SourceDateList);
        sortListView.setAdapter(adapter);
        intitdata();
    }

    public void intitdata() {
        List<String> strings = new ArrayList<>();
        strings.add("深圳");
        strings.add("北京");
        strings.add("上海");
        strings.add("广州");
        strings.add("西安");
        strings.add("黑龙江");
        strings.add("杭州");
        strings.add("南京");
        strings.add("重庆");
        strings.add("成都");
        strings.add("福州");
        strings.add("浙江");
        strings.add("宁夏");

        SourceDateList = filledData(strings);
        // 根据a-z进行排序源数据
        Collections.sort(SourceDateList, pinyinComparator);
        adapter.updateListView(SourceDateList);

    }

    /**
     * 为ListView填充数据
     *
     * @param list
     * @return
     */
    private List<SortModel> filledData(List<String> list) {
        List<SortModel> mSortList = new ArrayList<SortModel>();

        for (int i = 0; i < list.size(); i++) {
            SortModel sortModel = new SortModel();
            sortModel.setName(list.get(i));
//            sortModel.setId(list.get(i).getId());
            //汉字转换成拼音
            String pinyin = characterParser.getSelling(list.get(i));
            String sortString = pinyin.substring(0, 1).toUpperCase();

            // 正则表达式，判断首字母是否是英文字母
            if (sortString.matches("[A-Z]")) {
                sortModel.setSortLetters(sortString.toUpperCase());
            } else {
                sortModel.setSortLetters("#");
            }

            mSortList.add(sortModel);
        }
        return mSortList;

    }
}
